package org.xshop.scheduler;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.xshop.mapper.TOrderExpressMapper;
import org.xshop.model.TGoods;
import org.xshop.model.TOrder;
import org.xshop.model.TOrderSku;
import org.xshop.service.impl.ExpressmanService;
import org.xshop.service.impl.GoodsService;
import org.xshop.service.impl.OrderService;
import org.xshop.service.impl.OrderSkuService;
import org.xshop.service.impl.SysService;

import top.ibase4j.core.Constants;
import top.ibase4j.core.support.generator.Sequence;
import top.ibase4j.core.util.CacheUtil;
import top.ibase4j.core.util.DateUtil;
import top.ibase4j.core.util.InstanceUtil;

@Component
@EnableScheduling
public class OrderTask {
    @Autowired
    private OrderService orderService;
    @Autowired
    private SysService sysService;
    @Autowired
    private OrderSkuService orderSkuService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ExpressmanService expressmanService;
    @Autowired
    private TOrderExpressMapper orderExpressMapper;

    public void autoOrder() {
        Map<String, Object> params = InstanceUtil.newHashMap();
        params.put("express", "1");
        params.put("orderBy", "o.id_");
        List<Long> ids = orderExpressMapper.queryAvailableOrderId(params);
        for (Long id : ids) {
            TOrder order = new TOrder();
            order.setId(id);
            order = orderService.selectOne(order);
            params = InstanceUtil.newHashMap();
            expressmanService.query(params);
        }
    }

    /**
     * 订单失效
     */
    @Transactional
    @Scheduled(cron = "1 0 0 * * *")
    public void expire() {
        String v = sysService.getSysParam("ORDER-NO-PAY-DAYS", "3");
        Map<String, Object> params = InstanceUtil.newHashMap();
        params.put("state", "1");
        String requestId = Sequence.next().toString();
        params.put("endOderTime", DateUtil.addDate(new Date(), Calendar.DAY_OF_MONTH, -Integer.valueOf(v)));
        List<TOrder> list = orderService.queryList(params);
        for (TOrder order : list) {
            order.setState("2");
            TOrderSku entity = new TOrderSku();
            entity.setOrderId(order.getId());
            List<TOrderSku> skus = orderSkuService.queryList(entity);
            for (TOrderSku sku : skus) {
                String cacheKey = Constants.CACHE_NAMESPACE + "ODR-GD-" + sku.getSkuId();
                if (CacheUtil.getLock(cacheKey, requestId, 60 * 60 * 24)) {
                    try {
                        TGoods goods = goodsService.queryById(sku.getSkuId());
                        goods.setStock(goods.getStock().add(sku.getQuantity()));
                        goodsService.updateData(goods);
                    } finally {
                        CacheUtil.unLock(cacheKey, requestId);
                    }
                }
            }
        }
        orderService.updateBatch(list);
    }
}
